﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using FlyingAPI;
using System.Net;
using FlickrNet;
/*

namespace TagSpider
{
    class TagDownloadThread
    {
        public enum TagDownloadStatus { STATUS_STOP, STATUS_PAUSE, STATUS_WORKING };

        LogAppend WritelnLog;
        JobPage mainJobPage;

        public TagDownloadThread(LogAppend g, TagDownloadLog downloadLog, TagDownloadController controller)
        {
            m_downloadLog       = downloadLog;
            m_controller        = controller;
            WritelnLog = g;


            // 初始化状态，创建线程入口
            m_bDownloading      = false;
            m_DownloadStatus    = TagDownloadStatus.STATUS_STOP;
 //           m_entryPoint        = new ThreadStart(threadProc);
            m_entryPoint = new ThreadStart(threadProc);
        }

        public void start()
        {
            m_DownloadStatus = TagDownloadStatus.STATUS_WORKING;

            // 创建线程，并设为背景线程，当主线程退出，背景线程也会退出
            m_workThread = new Thread(m_entryPoint);
            m_workThread.IsBackground = true;
            m_workThread.Start();
        }

        // 不应该被执行
        public void pause()
        {
            m_DownloadStatus = TagDownloadStatus.STATUS_PAUSE;
        }

        // 不应该被执行，使用abort()
        public void stop()
        {
            // 设置为STATUS_STOP并不能保证当前就已
            // 经停止了，所以后面需要等待真正退出
            m_DownloadStatus = TagDownloadStatus.STATUS_STOP;

            // 等待直到真正退出才返回
            while (m_bDownloading)
                Thread.Sleep(100);
        }

        // 不应该被执行
        public void resume()
        {
            m_DownloadStatus = TagDownloadStatus.STATUS_WORKING;
        }

        public void abort()
        {
            m_workThread.Abort();

            // 复原状态位
            m_DownloadStatus = TagDownloadStatus.STATUS_STOP;
            m_bDownloading = false;
        }

        public TagDownloadStatus getCurStatus()
        {
            return m_DownloadStatus;
        }

        public bool isDownloading()
        {
            return m_bDownloading;
        }

        private void threadProc()
        {
            JobOnce(this.Url);
        }

        public void JobOnce(String url)
        {
            FlyingPig flyingpig = new FlyingPig();
            //经由API调用连接服务器


            //debug
            int total = 0;
            //debug

            try
            {
                WritelnLog("Connecting Server and Retrieving JobPage...");
                flyingpig.setConnect(url);
                List<SearchJob> searchJobs = flyingpig.requestSearchJobs(10);
                for (int i = 0; i < searchJobs.Count;i++ )
                {
                    if (searchJobs[i].Concept.Equals("animal"))
                    {
                        mainJobPage = flyingpig.requestJobPage(searchJobs[i].SearchJobId);

                        while (mainJobPage != null)
                        {
                            WritelnLog("Page:" + mainJobPage.Page);
                            WritelnLog("SearchJobID:" + mainJobPage.SearchJobId);
                            WritelnLog("ClientID" + mainJobPage.ClientID);
                            WritelnLog("PageState" + mainJobPage.PageState);

                            WritelnLog("Downloading ImageInfo...");
                            List<ImageInfo> list = downloadImageList(searchJobs[i].Concept, searchJobs[i].PageCapacity, mainJobPage.Id);
                            WritelnLog("Done.");

                            WritelnLog("Uploading ImageInfo...");
                            foreach (ImageInfo ii in list)
                            {
                                //经由API调用将ImageInfo上传至服务器
                                WritelnLog("ImageInfo, url: " + ii.Url + "pageID: " + ii.PageId);
                                FlyingPigSTATUS status = flyingpig.uploadImageInfo(ii);
                                WritelnLog(status.ToString());
                                if (status.Equals(FlyingPigSTATUS.SERVER_INNER_FAILED))
                                {
                                    break;
                                }
                            }
                            WritelnLog("Done.");


                            mainJobPage = flyingpig.requestJobPage(searchJobs[i].SearchJobId);//继续获取JobPage
                        }

                    }
                }

                WritelnLog("Done." + total + "ImageInfo Uploaded.");
            }
            catch (WebException we)
            {
                WritelnLog(we.Message);
                return;
            }
        }

        public List<ImageInfo> downloadImageList(String concept, int nMaxDownload,int pageID)
        {
            List<ImageInfo> list = new List<ImageInfo>();

            //使用Flickr API KEY，可能过期，过期后需要重新获得
            Flickr flickr = new Flickr("a409dac796e9e0d196f1c6b42dd1d370", "567c8653a0a4d942");
            PhotoSearchOptions options = new PhotoSearchOptions();

            concept = concept.Replace("_", ",");
            options.Tags = concept;
            options.PerPage = nMaxDownload;

            Photos photos = flickr.PhotosSearch(options);

            options.PerPage = 500;
            options.Page = mainJobPage.Page;
            Photos photosPerPage = flickr.PhotosSearch(options);
            foreach (Photo photo in photosPerPage.PhotoCollection)
            {
                try
                {
                    String tags = String.Empty;
                    PhotoInfoTag[] photoTags = flickr.TagsGetListPhoto(photo.PhotoId);
                    foreach (PhotoInfoTag photoTag in photoTags)
                    {
                        tags += photoTag.TagText + ",";
                    }
                    tags = tags.TrimEnd(",".ToCharArray()).Trim();
                    string photoUrl = String.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}.jpg", photo.Farm, photo.Server, photo.PhotoId, photo.Secret);
                    list.Add(new ImageInfo(photoUrl, photo.WebUrl, tags, photo.PhotoId, photo.UserId, concept, pageID));//此处临时添加 需要修改

                }
                catch (Exception e)
                {
                    continue;
                }

            }


            return list;
        }

        private TagDownloadLog m_downloadLog;
        private TagDownloadStatus m_DownloadStatus;
        private ThreadStart m_entryPoint;
        private Thread m_workThread;
        private bool m_bDownloading;
        private TagDownloadController m_controller;
        private int processID;
        private String m_url;
        public String Url
        {
            get { return m_url; }
            set { m_url = value; }
        }

    }
}

*/